package com.chuan.it_market.controller.buyer_controller;

import com.alibaba.fastjson.JSON;
import com.chuan.it_market.controller.BaseController;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Address;
import com.chuan.it_market.pojo.entity.Goods;
import com.chuan.it_market.pojo.entity.UserOrder;
import com.chuan.it_market.pojo.vo.ShopCarVO;
import com.chuan.it_market.pojo.vo.UserOrderVO;
import com.chuan.it_market.service.AddressService;
import com.chuan.it_market.service.GoodsService;
import com.chuan.it_market.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/buyer/order")
public class OrderController extends BaseController {
    @Resource
    OrderService orderService;
    @Resource
    AddressService addressService;
    @Resource
    GoodsService goodsService;

    //从购物车进入订单确认
    @RequestMapping("insertPre")
    String insertPre(String orderInfoStr,Double allCost,Model model){
            //将后台传来的Json字符串转型为数组
            List<ShopCarVO> carVOs = JSON.parseArray(orderInfoStr, ShopCarVO.class);
            model.addAttribute("cars",carVOs);
            model.addAttribute("allCost",allCost);
            model.addAttribute("orderInfoStr",orderInfoStr);
            //查询用户地址
            List<Address> addressList=addressService.selectAddressByUserno(getUsreId());
            model.addAttribute("address",addressList);
            return "buyer/orders/order-addpre";
    }

    //生成订单的方法
    @RequestMapping("insert")
    @ResponseBody
    ResponseDTO insert(String orderInfos,String address){
        List<ShopCarVO> carVOS=JSON.parseArray(orderInfos,ShopCarVO.class);


        UserOrder userOrder=new UserOrder();
        Goods goods=new Goods();
        // String orderId=System.currentTimeMillis()+ MathUtil.getRandomStr(3);
        //userOrder.setOrderId(orderId);

        userOrder.setStatus("待发货");
        userOrder.setBno(getUsreId());
        userOrder.setUserAddress(address);
        userOrder.setPayTime(new Date());
        int i=1;
        for(ShopCarVO s:carVOS){
            double k=s.getGoods().getInventory()-s.getCount();
            if(k<0){return ResponseDTO.fail("商品库存不足");}
        }

        for(ShopCarVO s:carVOS){

            double k=s.getGoods().getInventory()-s.getCount();
            goods.setGno(s.getGno());
            goods.setInventory(k);

            goodsService.ReCount(goods);

            if(i!=0){
                userOrder.setCount(s.getCount());
                userOrder.setGno(s.getGno());
                userOrder.setSno(s.getSno());
                userOrder.setTotalPrice(s.getTotalPrice());
                userOrder.setTag("0");
                i=orderService.insert(userOrder);

            }
            else {

                return ResponseDTO.fail("增加失败");
            }
        }




        return ResponseDTO.ok("增加成功");

//        ResponseDTO res=orderService.addOrder(userOrder,carVOS);
    }

    //买家查询订单的方法 将根据bno查
    @RequestMapping("orderList")
    String orderList(Model model){
        List<UserOrderVO> userOrderVOList=orderService.orderList(getUsreId());
        model.addAttribute("orderList",userOrderVOList);
        return "buyer/orders/order-list";
    }

    //卖家查询订单的方法 将根据sno查
    @RequestMapping("orderList2")
    String orderList2(Model model){
        List<UserOrderVO> userOrderVOList=orderService.orderList2(getUsreId());
        model.addAttribute("orderList",userOrderVOList);
        return "seller/orders/order-list";
    }

    @RequestMapping("orderList3")
    String orderList3(Model model){
        return "seller/center/seller-statistics";
    }

    @RequestMapping("receive/{ono}")
    @ResponseBody
    ResponseDTO receive(@PathVariable Integer ono){
        int res=orderService.receive(ono);
        if(res!=0){
            return ResponseDTO.ok("收货成功");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("receive1/{ono}")
    @ResponseBody
    ResponseDTO receive1(@PathVariable Integer ono){
        int res=orderService.receive1(ono);
        if(res!=0){
            return ResponseDTO.ok("发货成功");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("back/{ono}")
    @ResponseBody
    ResponseDTO back(@PathVariable Integer ono){
        int res=orderService.back(ono);
        if(res!=0){
            return ResponseDTO.ok("已提交退货申请");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("back1/{ono}")
    @ResponseBody
    ResponseDTO back1(@PathVariable Integer ono){
        int res=orderService.back1(ono);
        if(res!=0){
            return ResponseDTO.ok("接收退货成功");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("back2/{ono}")
    @ResponseBody
    ResponseDTO back2(@PathVariable Integer ono){
        int res=orderService.back2(ono);
        if(res!=0){
            return ResponseDTO.ok("已拒绝退货申请");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("delete1/{ono}")
    @ResponseBody
    ResponseDTO delete1(@PathVariable Integer ono){
        int res=orderService.delete1(ono);
        if(res!=0){
            return ResponseDTO.ok("删除订单成功");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("delete2/{ono}")
    @ResponseBody
    ResponseDTO delete2(@PathVariable Integer ono){
        int res=orderService.delete2(ono);
        if(res!=0){
            return ResponseDTO.ok("删除订单成功");
        }
        else{
            return ResponseDTO.fail("操作失败");
        }
    }

    @RequestMapping("selectOrderByStatus/{status}")
    String selectOrderByStatus(@PathVariable String status,Model model){
        Integer bno=getUsreId();
        if(status.equals("全部")){
            List<UserOrderVO> userOrderVOList=orderService.orderList(bno);
            model.addAttribute("orderList",userOrderVOList);
            return "buyer/orders/order-list";
        }

        List <UserOrderVO> userOrderVOList=orderService.selectOrderByStatus(status,bno);
        model.addAttribute("orderList",userOrderVOList);
        return "buyer/orders/order-list";
    }



}
